// pages/user-course/user-course-detail/course-schedule/course-schedule-detail/course-schedule-detail.js
const app = getApp()
const constant = require("../../../../../constant.js")
const storage = require("../../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    qiniu: constant.resource.qiniu
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.scheduleId = options.scheduleId;
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url,
      unionid: wx.getStorageSync(storage.keys.unionid)
    })

    if(options.from) {
      this.setData({
        last: options.from
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: that.data.host + '/profile/course-schedules/' + that.data.scheduleId + '?unionid=' + that.data.unionid,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            schedule: res.data.data.courseSchedule,
            courseName: res.data.data.courseName,
            coachAvatar: res.data.data.coachAvatar,
            couldEdit: res.data.data.couldEdit,
            today: res.data.data.today,
            couldCancel: res.data.data.couldCancel,
            couldSignin: res.data.data.couldSignin,
            roundStatus: res.data.data.roundStatus,
            scheduleStatus: res.data.data.scheduleStatus,
            hasSigninRecord: res.data.data.hasSigninRecord,
            give: res.data.data.give,
            content:res.data.data.content
          });

          if (res.data.data.session) {
            that.setData({
              session: res.data.data.session,
              coursewares: res.data.data.coursewares
            })
          }

          //that.validateSignin("10|1589359907150");

        } else {
          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '加载数据失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  onEdit: function () {
    var that = this;

      wx.navigateTo({
        url: '../../course-appoint/course-appoint?courseRoundId=' + that.data.schedule.course_round + '&scheduleId=' + that.data.scheduleId
      })
  },
//增加了一个message做取消预约时做签到销课测试
  onCancel: function () {
    var that = this;
    if(that.data.today == 'true' || that.data.today == true) {
      that.cancelToday();
    } else {
      that.cancelOtherDay();
    }
    
  },

  cancelOtherDay: function() {
    var that = this;
    wx.showModal({
      title: '取消预约？',
      content: "是否确认取消预约该课程？",
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: that.data.host + '/profile/courses/' + that.data.schedule.course_round + '/schedules/' + that.data.scheduleId + '/cancel?unionid=' + that.data.unionid,
            method: 'POST',
            data: {
              course_round: that.data.schedule.course_round,
              course_schedule: that.data.scheduleId,
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log('done');
                wx.showToast({
                  title: '操作成功',
                  icon: 'success',
                  duration: 2000,
                  mask: true,
                  success: function () {
                    wx.navigateBack({
                      delta: 3
                    });
                  }
                });
              
            },
            fail: function (res) {
              wx.showToast({
                title: '取消预约失败',
                icon: 'none',
                duration: 3000,
                mask: true
              })
            }
          });
        }
      }
    })
  },

  cancelToday: function() {
    var that = this;
    wx.request({
      url: that.data.host+'/xh/api/course/get_cancel/', //仅为示例，非真实的接口地址
      method: 'POST',
      data: {
        course_round: that.data.schedule.course_round,
      },
      dataType: 'json',
      responseType: 'text',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        // console.log(res.data);
        var content = '';
        if (res.data==0){
          // cotent = '你本月还有3次取消/逾期机会';
          that.setData({
            cotent : '本月还能取消3次，但你失去了一次见教练的机会'
          })
        }
        if (res.data==1){
          // cotent = '你本月还有2次取消/逾期机会';
          that.setData({
            cotent : '本月只剩2次取消的机会，赶紧安排下一节课的时间'
          })
        }
        if (res.data==2){
          // cotent = '你本月还有1次取消/逾期机会,建议您更换课程时间';
          that.setData({
            cotent : '就剩1次取消的机会了，您的教练在哭泣，赶紧换个时间安慰一下'
          })
        }
        if (res.data==3){
          that.setData({
            cotent :'你没机会了，去拜见教练吧～'

          })
        }
        if(res.data < 3) {
          wx.showModal({
            title: '取消预约？',
            content: that.data.cotent ,
            success: function (res) {
              if (res.confirm) {
                wx.request({
                  url: that.data.host + '/profile/courses/' + that.data.schedule.course_round + '/schedules/' + that.data.scheduleId + '/cancel?unionid=' + that.data.unionid,
                  method: 'POST',
                  data: {
                    course_round: that.data.schedule.course_round,
                    course_schedule: that.data.scheduleId,
                  },
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    console.log('done');
                      wx.showToast({
                        title: '操作成功',
                        icon: 'success',
                        duration: 2000,
                        mask: true,
                        success: function () {
                          wx.navigateBack({
                            delta: 3
                          });
                        }
                      });
                    
                  },
                  fail: function (res) {
                    wx.showToast({
                      title: '取消预约失败',
                      icon: 'none',
                      duration: 3000,
                      mask: true
                    })
                  }
                });
              }
            }
          })
        } else {
          wx.showModal({
            title: '取消预约？',
            content: that.data.cotent ,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '加载数据失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  scanCourse: function () {
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        that.validateSignin(res.result);
      }
    })
  },

  validateSignin: function(message) {
    var that = this;
    that.data.message = message;
    wx.request({
      url: that.data.host + '/signin/course-schedules/' + that.data.scheduleId + '/validate-signin?unionid=' + that.data.unionid + '&message=' + message,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == "0") {
          wx.showModal({
            title: '签到',
            content: '是否确认签到？',
            success: function () {
              that.signin(that.data.message);
            }
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '签到验证失败',
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    });
  },

  

  signin: function(message) {
    var that = this;
    wx.request({
      url: that.data.host + '/signin/schedules/' + that.data.scheduleId + '/confirm?unionid=' + that.data.unionid + '&message=' + message,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == "0") {
          wx.navigateTo({
            url: '../course-schedule-signin-success/course-schedule-signin-success?scheduleId=' + that.data.scheduleId
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '签到失败',
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    });
  }
})